﻿@*@model Knockout.Message
@(Html.UpshotContext().DataSource<OpenWeb.Site.Controllers.MessageController>(x=>
    x.GetMessages()) 
 )*@


@{
    string indexURL = @Url.Action("Message", "api");
    ViewBag.Title = "Index";
    new UrlHelper(this.ViewContext.RequestContext);
 }


@model IEnumerable<OpenWeb.Dal.Models.Message>

@{
    ViewBag.Title = "Index";
}

MY
@myHelpers.SetUrlAndAction(this,"~/Message/Edit")

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Messaggio)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RichiediConferma)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Messaggio)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RichiediConferma)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>


<p>
    @*@Html.ActionLink("Create New", "Create")*@
</p>

<div class="main-content">
        <div>
            <h1>All Products</h1>
            <input type="button" value="Get" onclick="getAll();" />
            <ul id="messages"/>
        </div>
        <div>
            <label for="prodId">ID:</label>
            <input type="text" id="prodId" size="5"/>
            <input type="button" value="Search" onclick="find();" />
            <p id="message" />
        </div>
    </div>


<script type="text/javascript">

    function GetActions(id) {
        var url = '  | ' +
            '@Html.ActionLink("Edit", "Edit/0") | ' +
            '@Html.ActionLink("Details", "Details/0") | ' +
            '@Html.ActionLink("Delete", "Delete/0") | ';

        url = url.replace('0', id.toString());

        return url;
    }

    function getAll() {

            // Send an AJAX request
            $.getJSON("api/message/",
            function (data) {

                //alert(data == null);               
                //alert("in get data, tot elementi : " + data.length.toString() + 
                //  ", el 1 : "+  data[0].ID.toString() );

                $.each(data, 
                    function (intIndex, obj) {

                        var str = obj.ID.toString() + ': ' + obj.Messaggio;
                          //  + GetActions(obj.ID.toString()).ht;

                        $('#messages').append($('<li>')).append(str)
                            .append(GetActions(obj.ID));

                        //$('<li/>', { text: str }).appendTo($('#messages'));
                       
                })
                .fail(
                function (jqXHR, textStatus, err) {
                    $('#message').text('Error: ' + err);
                });
            })
                
           }
        
   
        getAll();
   

        function find() {

            prompt("ok in find", "ok");

            var id = $('#prodId').val();
            $.getJSON("api/message/" + id,
                function (data) {
                    var str = data.ID.toString() + ': ' + data.Messaggio;
                    $('#message').text(str);
                })
            .fail(
                function (jqXHR, textStatus, err) {
                    $('#message').text('Error: ' + err);
                });
        }

    //});

</script>

@*<script type="text/javascript">
    //for knock & Upshoot

    $(function () {

      function knockVIewModel()
        {
        var self = this.
        self.dataSource = upshot.dataSources.AllProducts().refresh();
        self.products = self.dataSource.getEntities();
        
        }

        function Message(data) {
            this.ID = ko.observable(data.ID || "");
            this.InviatoDa = ko.observable(data.InviatoDa || "");
            this.Messaggio = ko.observable(data.Messaggio || "");
            this.RichiediConferma = ko.observable(data.RichiediConferma) || "";

            //this.Destinatari = ko.observableArray(data.Destinatari ?
            //    $.map(data.Destinatari, function (item)
            //{ return new UserProfile(item) }) : []);
        }

        function UserProfile(data) {
            this.Number = ko.observable(data.Number || "");
            this.Description = ko.observable(data.Description || "");
        }

        function MessageViewModel() {
            var self = this;
            self.Messaggi = ko.observableArray([]);

            $.ajax("@indexURL", {
                type: "get", contentType: "application/json",
                success: function (result) {
                    $.map(result,
                        function (item)
                        { self.Messaggi.push(new Message(item)) });
                }
            });

            self.save = function () {
                $.ajax("@indexURL", {
                    data: ko.toJSON(self.Messaggi),
                    type: "post", contentType: "application/json",
                    success: function (result) { alert(result) }
                });
            };
            self.addMessaggio = function () {
                self.Messaggi.push(new Ticket({}));
            };
            //self.addPhone = function () {
            //    this.Phones.push(new Phone({}));
            //};
        };


        ko.applyBindings(MessageViewModel());

    });

</script>*@


<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Messaggio)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.RichiediConferma)
        </th>
        <th></th>
    </tr>

   <tbody data-bind="foreach: Tickets">
    
        <tr>
            <td>
                 <input data-bind="value: Messaggio">
            </td>
            <td>
               <input data-bind="value: RichiediConferma" />
            </td>
            <td>
                 Delete <span data-bind="text: Name"></span>
                @*<button data-bind="click: addPhone">Add Phone</button>*@

             @*   @Html.ActionLink("Edit", "Edit", new { id = item.ID }) |
            @Html.ActionLink("Details", "Details", new { id = item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.ID })*@
            </td>
        </tr>
    </tbody>

</table>
<br />
<button data-bind="click: addMessaggio">Add new item</button>
 
<button data-bind="click: save">Save</button>



